package com.zwb.service.controller;


import com.zwb.service.pojo.Admin;
import com.zwb.service.pojo.Menu;
import com.zwb.service.service.IMenuService;
import com.zwb.service.common.CacheConstant;
import com.zwb.service.vo.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zwb
 * @since 2022-08-23
 */
@Api(tags = "菜单管理")
@CacheConfig(cacheNames = CacheConstant.CACHEABLE_VALUE_3_DAT)
@RestController
@RequestMapping("/system/cfg")
public class MenuController {

    @Autowired
    private IMenuService menuService;

    // "cacheable:3day::getMenuByAdminId:userid:1"
    @Cacheable(keyGenerator = "userIdKeyGenerator")
    @ApiOperation(value = "通过用户id查询菜单列表")
    @GetMapping("/menu")
    public R getMenuByAdminId() {
        // 获取security 全局上下文对象
        Admin admin = (Admin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<Menu> data = menuService.getMenusByAdminId(admin.getId());
        return R.success(data);
    }

}
